<!DOCTYPE html>
[#escape x as (x)!?html]
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <title>密码修改 - ${site.seoTitle} - Powered by JPROCMS</title>
    [#include '../includes/common_res.html'/]
    <script src="${resource}/static/plugins/crypto-js/crypto-js.min.js"></script>
    <script src="${resource}/static/plugins/sm-crypto/dist/sm2.js"></script>
  </head>
  <body>
    [#assign shadowNav=true/] [#include '../includes/header.html'/]
    <div class="container mt-3">
      <div class="row">
        <div class="col-sm-3">
          <div class="list-group mt-2">
            [#assign settings = 'passwd'/] [#include 'user_left.html'/]
          </div>
        </div>
        <div class="col-sm-9">
          <h3 class="py-3 border-bottom">修改密码</h3>
          <div
            class="alert alert-danger d-none"
            id="passwordFormError"
            role="alert"
          ></div>
          <form
            class="mt-3"
            id="passwordForm"
            action="${api}/member/update-password"
            method="post"
          >
            <div class="form-group">
              <label for="oldPassword">原密码</label>
              <input
                type="password"
                class="form-control"
                id="oldPassword"
                name="oldPassword"
                required
              />
            </div>
            <div class="form-group">
              <label for="newPassword">新密码</label>
              <input
                type="password"
                class="form-control"
                id="newPassword"
                name="newPassword"
                required
              />
            </div>
            <div class="form-group">
              <label for="confirmNewPassword">确认新密码</label>
              <input
                type="password"
                class="form-control"
                id="confirmNewPassword"
                required
                data-rule-equalTo="#newPassword"
                data-msg-equalTo="两次输入不相同"
              />
            </div>
            <button type="submit" class="btn btn-primary">提交</button>
          </form>
        </div>
      </div>
    </div>
    [#include '../includes/footer.html'/] [#include '../includes/inc_msg.html'/]
    <script>
      var publicKey = "";
      axios
        .get("${api}/user/get-client-public-key", {
          headers: { "Tenant-Id": "1" },
        })
        .then(function (response) {
          publicKey = response.data.data;
        });

      function sm2Encrypt(message) {
        // 1 - C1C3C2，0 - C1C2C3，默认为1
        const cipherMode = 1;
        let encryStr = sm2.doEncrypt(message, publicKey, cipherMode);
        return encryStr;
      }

      $(function () {
        Cms.getLoginUser("${base}", "${api}", true);
        $("#passwordForm").validate({
          submitHandler: function (form, event) {
            event.preventDefault();
            const token = sessionStorage.getItem("accessToken");
            var body = $(form).serializeJSON();
            if (body.oldPassword) {
              body.oldPassword = sm2Encrypt(body.oldPassword);
            }
            body.newPassword = sm2Encrypt(body.newPassword);
            axios
              .post(form.action, body, {
                headers: {
                  "Tenant-Id": Cms.getTenantId(),
                  Authorization: "Bearer " + token,
                },
              })
              .then(function (response) {
                var data = response.data;
                if (data === null) return;
                var $passwordFromError = $("#passwordFormError");
                if (data.code !== 0) {
                  $passwordFromError.text(data.msg);
                  $passwordFromError.removeClass("d-none");
                  return;
                }
                $passwordFromError.addClass("d-none");
                successTip();
                location.reload();
              });
          },
        });
      });
    </script>
  </body>
</html>
[/#escape]
